<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <title>登录</title>
  <link rel="icon" href="/static/img/favicon.png">
  <link rel="stylesheet" href="/static/vendor/bootstrap.min.css">
  <script src="/static/vendor/bootstrap.bundle.min.js"></script>
  <script src="/static/vendor/jquery.js"></script>
  <style>
    main {
      background: url('/static/img/bg.webp');
      background-repeat: no-repeat;
      background-size: cover;
    }
  </style>
</head>

<body>
  <main>
    <div class="container">
      <section class="section register min-vh-100 d-flex flex-column align-items-center justify-content-center py-4">
        <div class="container">
          <div class="row justify-content-center">
            <div class="col-lg-4 col-md-6 d-flex flex-column align-items-center justify-content-center">
              <div class="card mb-3">
                <div class="card-body">
                  <div class="pt-4 pb-2">
                    <h5 class="card-title text-center pb-0 fs-4">注册用户</h5>
                    <p class="text-center small" id="error" style="color:red"></p>
                  </div>
                  <form method="POST" action="/app/register" class="row g-3 needs-validation" id="form" novalidate>
                    <div class="col-12">
                      <label for="yourUsername" class="form-label">用户名</label>
                      <div class="input-group has-validation">
                        <input type="text" name="username" class="form-control" id="yourUsername" required>
                        <div class="invalid-feedback">请输入用户名</div>
                      </div>
                    </div>
                    <div class="col-12">
                      <label for="yourPassword" class="form-label">密码</label>
                      <input type="password" name="password" class="form-control" id="yourPassword" required>
                      <div class="invalid-feedback">请输入密码</div>
                    </div>
                    <div class="col-12">
                      <label for="confirmPassword" class="form-label">确认密码</label>
                      <input type="password" name="confirmPassword" class="form-control" id="confirmPassword" required>
                      <div class="invalid-feedback">请再次输入密码</div>
                    </div>
                    <div class="col-12">
                      <button class="btn btn-primary w-100" type="submit">创建用户</button>
                    </div>
                    <div class="col-12">
                      <p class="small mb-0">已有账号? <a href="/app/login">登录</a></p>
                    </div>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
    </div>
    </section>
    </div>
  </main>
  <script>
    $("#form").on("submit", function (e) {
      e.preventDefault();

      if (!this.checkValidity()) {
        $(this).addClass('was-validated');
        return;
      }

      $(this).addClass('was-validated');

      $.ajax({
        url: "/app/register/",
        type: "POST",
        data: {
          username: $("#yourUsername").val(),
          password: $("#yourPassword").val(),
          confirmPassword: $("#confirmPassword").val(),
        },
        success: function (res) {
          if (res.status === "ok") {
            window.location.href = "/app/login/";
          } else {
            $("#error").text(res.msg);
          }
        }
      });
    });
    
  </script>
</body>

</html>